package com.a21vianet.quincysx.demo.binderpool;

import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.a21vianet.quincysx.demo.binderpool.pool.BinderPool;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
//        onWork();
        new Thread(new Runnable() {
            @Override
            public void run() {
                onWork();
            }
        }).start();
    }

    /**
     * 一定在子线程中运行否则就会出现死锁
     */
    private void onWork() {
        BinderPool binderPool = BinderPool.getInstance(MainActivity.this);
        Log.e("====", "onClick:" + Thread.currentThread().getName());
        IBinder securityBinder = binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);
        ISecurityCenter iSecurityCenter = ISecurityCenter.Stub.asInterface(securityBinder);
        try {
            String msg = "Hello Android";
            String encypt = iSecurityCenter.encypt(msg);
            Log.e("==加密后==", encypt);
            String decrypt = iSecurityCenter.decrypt(encypt);
            Log.e("==解谜后==", decrypt);

            IBinder computeBinder = binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
            ICompute iCompute = ICompute.Stub.asInterface(computeBinder);

            int add = iCompute.add(1, 200);
            Log.e("==算数==", "1+200=" + add);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
